package org.aeon.colorfulplanebattle.flying;

import org.aeon.colorfulplanebattle.flying.entity.FlyObject;
import org.aeon.colorfulplanebattle.flying.entity.Plane;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Properties;
import java.util.Random;

/**
 * @author: MissingAeon
 * @date: 2024/1/9 19:42
 * @description:
 */
public class Utils {
    /**
     * 碰撞监测
     * 将坐标生成矩形判断是否相交
     *
     * @param obj1 飞行物
     * @param obj2 飞行物
     * @return
     */
    public static boolean isEdgeCollision(FlyObject obj1, FlyObject obj2) {
        Rectangle o1 = new Rectangle(obj1.getX() + (obj1.getWidth() - obj1.getTouchWidth()) / 2,
                obj1.getY() + (obj1.getHeight() - obj1.getTouchHeight()) / 2, obj1.getTouchWidth(), obj1.getTouchHeight());
        Rectangle o2 = new Rectangle(obj2.getX() + (obj2.getWidth() - obj2.getTouchWidth()) / 2,
                obj2.getY() + (obj2.getHeight() - obj2.getTouchHeight()) / 2, obj2.getTouchWidth(), obj2.getTouchHeight());
        return o1.intersects(o2);
    }


    /**
     * 翻转图片
     */
    public static BufferedImage flippedImage(BufferedImage picture) {
        AffineTransform transform = new AffineTransform(1, 0, 0, -1, 0, picture.getHeight() - 1);// 垂直翻转
        AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
        return op.filter(picture, null);
    }

    /**
     * 打乱数组
     *
     * @param array
     */
    public static void shuffleArray(Object[] array) {
        Random rand = new Random();

        for (int i = array.length - 1; i > 0; i--) {
            // Generate a random index j such that 0 <= j <= i
            int j = rand.nextInt(i + 1);

            // Swap array[i] and array[j]
            Object temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }

    public static Properties getProp(Class<?> clazz) {
        return getProp(clazz.getSimpleName());
    }

    public static Properties getProp(String name) {
        Properties prop = new Properties();
        try {
            InputStream is = Plane.class.getClassLoader()
                    .getResourceAsStream("config/" + name + ".properties");
            prop.load(is);
        } catch (IOException e) {
            throw new RuntimeException("配置信息读取失败！");
        }
        return prop;
    }

    public static <T> void stuffing(Properties prop, T t) {
        try {
            Class<?> clazz = t.getClass();
            String prefix = clazz.getSimpleName().substring(0, 1).toLowerCase() + clazz.getSimpleName().substring(1) + ".";
            while (clazz.getSuperclass() != null) {
                Field[] fields = clazz.getDeclaredFields();
                if (fields.length > 0) {
                    for (Field field : fields) {
                        String v = (String) prop.get(prefix + field.getName());
                        if (v == null || "".equals(v)) continue;
                        Class<?> fieldType = field.getType();
                        field.setAccessible(true);
                        // 根据字段的类型进行相应的类型转换
                        if (fieldType == int.class || fieldType == Integer.class) {
                            int intValue = Integer.parseInt(v);
                            field.set(t, intValue);
                        } else if (fieldType == boolean.class || fieldType == Boolean.class) {
                            boolean doubleValue = Boolean.parseBoolean(v);
                            field.set(t, doubleValue);
                        } else if (fieldType == long.class || fieldType == Long.class) {
                            long doubleValue = Long.parseLong(v);
                            field.set(t, doubleValue);
                        }
                    }
                }
                clazz = clazz.getSuperclass();
            }
            System.out.println(t);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("属性填充错误！");
        }
    }
}
